package com.e.library.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.e.library.listener.EListener;
import com.e.library.utils.EViewUtils;

import java.util.List;

/**
 * Created by lxy on 2016/7/5.
 */
public abstract class EAdapter<Data> extends RecyclerView.Adapter<EAdapter.EHolder> {

    protected int invalid = -1;
    protected List<Data> mDatas;
    protected Context mContext;
    protected EListener<Data> onItemClickListener;

    public EAdapter(Context context, List<Data> datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public List<Data> get() {
        return mDatas;
    }

    public void set(List<Data> list){
        this.mDatas = list;
    }

    public boolean isEmpty() {
        if (mDatas == null) {
            return true;
        }
        return mDatas.isEmpty();
    }

    public Data item(int position) {
        return mDatas.get(position);
    }

    public void remove(Data data) {
        if (mDatas != null && data != null) {
            int position = mDatas.indexOf(data);
            remove(position);
        }
    }

    public void remove(int position) {
        if (mDatas != null) {
            mDatas.remove(position);
            notifyItemRemoved(position);
            if (position != mDatas.size()) {
                notifyItemRangeChanged(position, mDatas.size() - position);
            }
        }
    }

    public void refresh(List<Data> list) {
        mDatas.clear();
        if (list != null && !list.isEmpty()) {
            mDatas.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void append(Data data) {
        if (mDatas != null && data != null) {
            mDatas.add(data);
            notifyItemInserted(mDatas.size() - 1);
        }
    }

    public void append(List<Data> list) {
        if (list != null && !list.isEmpty()) {
            int start = mDatas.size() - 1;
            int itemCount = list.size();
            mDatas.addAll(list);
            notifyItemRangeInserted(start, itemCount);
        }
    }

    public void setItemClickListener(EListener<Data> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected void onBindItemClickListener(final View view, final Data data, final int position) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onInvoked(view, data, position);
                }
            }
        });
    }

    public static class EHolder extends RecyclerView.ViewHolder {
        public EHolder(View itemView) {
            super(itemView);
        }

        public <V extends View> V find(int id) {
            return EViewUtils.findView(itemView, id);
        }

    }
}
